home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / jpsrc2.zip / JPEGDATA.H < prev    next >
Text File  |  1991-12-11  |  36KB  |  841 lines

  1. /*
  2.  * jpegdata.h
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file defines shared data structures for the various JPEG modules.
  9.  */
  10.  
  11.  
  12. /*
  13.  * You might need to change some of the following declarations if you are
  14.  * using the JPEG software within a surrounding application program
  15.  * or porting it to an unusual system.
  16.  */
  17.  
  18.  
  19. /* If the source or destination of image data is not to be stdio streams,
  20.  * these types may need work.  You can replace them with some kind of
  21.  * pointer or indicator that is useful to you, or just ignore 'em.
  22.  * Note that the user interface and the various jrdxxx/jwrxxx modules
  23.  * will also need work for non-stdio input/output.
  24.  */
  25.  
  26. typedef FILE * JFILEREF;    /* source or dest of JPEG-compressed data */
  27.  
  28. typedef FILE * IFILEREF;    /* source or dest of non-JPEG image data */
  29.  
  30.  
  31. /* These defines are used in all function definitions and extern declarations.
  32.  * You could modify them if you need to change function linkage conventions,
  33.  * as is shown below for use with C++.  Another application would be to make
  34.  * all functions global for use with code profilers that require it.
  35.  * NOTE: the C++ test does the right thing if you are reading this include
  36.  * file in a C++ application to link to JPEG code that's been compiled with a
  37.  * regular C compiler.  I'm not sure it works if you try to compile the JPEG
  38.  * code with C++.
  39.  */
  40.  
  41. #define METHODDEF static    /* a function called through method pointers */
  42. #define LOCAL      static    /* a function used only in its module */
  43. #define GLOBAL            /* a function referenced thru EXTERNs */
  44. #ifdef __cplusplus
  45. #define EXTERN      extern "C"    /* a reference to a GLOBAL function */
  46. #else
  47. #define EXTERN      extern    /* a reference to a GLOBAL function */
  48. #endif
  49.  
  50.  
  51. /* Here is the pseudo-keyword for declaring pointers that must be "far"
  52.  * on 80x86 machines.  Most of the specialized coding for 80x86 is handled
  53.  * by just saying "FAR *" where such a pointer is needed.  In a few places
  54.  * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol.
  55.  */
  56.  
  57. #ifdef NEED_FAR_POINTERS
  58. #define FAR  far
  59. #else
  60. #define FAR
  61. #endif
  62.  
  63.  
  64.  
  65. /* The remaining declarations are not system-dependent, we hope. */
  66.  
  67.  
  68. /*
  69.  * NOTE: if you have an ancient, strict-K&R C compiler, it may choke on the
  70.  * similarly-named fields in compress_info_struct and decompress_info_struct.
  71.  * If this happens, you can get around it by rearranging the two structs so
  72.  * that the similarly-named fields appear first and in the same order in
  73.  * each struct.  Since such compilers are now pretty rare, we haven't done
  74.  * this in the portable code, preferring to maintain a logical ordering.
  75.  */
  76.  
  77.  
  78.  
  79. /* This macro is used to declare a "method", that is, a function pointer. */
  80. /* We want to supply prototype parameters if the compiler can cope. */
  81. /* Note that the arglist parameter must be parenthesized! */
  82.  
  83. #ifdef PROTO
  84. #define METHOD(type,methodname,arglist)  type (*methodname) arglist
  85. #else
  86. #define METHOD(type,methodname,arglist)  type (*methodname) ()
  87. #endif
  88.  
  89. /* Forward references to lists of method pointers */
  90. typedef struct external_methods_struct * external_methods_ptr;
  91. typedef struct compress_methods_struct * compress_methods_ptr;
  92. typedef struct decompress_methods_struct * decompress_methods_ptr;
  93.  
  94.  
  95. /* Data structures for images containing either samples or coefficients. */
  96. /* Note that the topmost (leftmost) index is always color component. */
  97. /* On 80x86 machines, the image arrays are too big for near pointers, */
  98. /* but the pointer arrays can fit in near memory. */
  99.  
  100. typedef JSAMPLE FAR *JSAMPROW;    /* ptr to one image row of pixel samples. */
  101. typedef JSAMPROW *JSAMPARRAY;    /* ptr to some rows (a 2-D sample array) */
  102. typedef JSAMPARRAY *JSAMPIMAGE;    /* a 3-D sample array: top index is color */
  103.  
  104.  
  105. #define DCTSIZE        8    /* The basic DCT block is 8x8 samples */
  106. #define DCTSIZE2    64    /* DCTSIZE squared; # of elements in a block */
  107.  
  108. typedef JCOEF JBLOCK[DCTSIZE2];    /* one block of coefficients */
  109. typedef JBLOCK FAR *JBLOCKROW;    /* pointer to one row of coefficient blocks */
  110. typedef JBLOCKROW *JBLOCKARRAY;        /* a 2-D array of coefficient blocks */
  111. typedef JBLOCKARRAY *JBLOCKIMAGE;    /* a 3-D array of coefficient blocks */
  112.  
  113. typedef JCOEF FAR *JCOEFPTR;    /* useful in a couple of places */
  114.  
  115.  
  116. /* The input and output data of the DCT transform subroutines are of
  117.  * the following type, which need not be the same as JCOEF.
  118.  * For example, on a machine with fast floating point, it might make sense
  119.  * to recode the DCT routines to use floating point; then DCTELEM would be
  120.  * 'float' or 'double'.
  121.  */
  122.  
  123. typedef JCOEF DCTELEM;
  124. typedef DCTELEM DCTBLOCK[DCTSIZE2];
  125.  
  126.  
  127. /* Types for JPEG compression parameters and working tables. */
  128.  
  129.  
  130. typedef enum {            /* defines known color spaces */
  131.     CS_UNKNOWN,        /* error/unspecified */
  132.     CS_GRAYSCALE,        /* monochrome (only 1 component) */
  133.     CS_RGB,            /* red/green/blue */
  134.     CS_YCbCr,        /* Y/Cb/Cr (also known as YUV) */
  135.     CS_YIQ,            /* Y/I/Q */
  136.     CS_CMYK            /* C/M/Y/K */
  137. } COLOR_SPACE;
  138.  
  139.  
  140. typedef struct {        /* Basic info about one component */
  141.   /* These values are fixed over the whole image */
  142.   /* For compression, they must be supplied by the user interface; */
  143.   /* for decompression, they are read from the SOF marker. */
  144.     short component_id;    /* identifier for this component (0..255) */
  145.     short component_index;    /* its index in SOF or cinfo->comp_info[] */
  146.     short h_samp_factor;    /* horizontal sampling factor (1..4) */
  147.     short v_samp_factor;    /* vertical sampling factor (1..4) */
  148.     short quant_tbl_no;    /* quantization table selector (0..3) */
  149.   /* These values may vary between scans */
  150.   /* For compression, they must be supplied by the user interface; */
  151.   /* for decompression, they are read from the SOS marker. */
  152.     short dc_tbl_no;    /* DC entropy table selector (0..3) */
  153.     short ac_tbl_no;    /* AC entropy table selector (0..3) */
  154.   /* These values are computed during compression or decompression startup */
  155.     long true_comp_width;    /* component's image width in samples */
  156.     long true_comp_height;    /* component's image height in samples */
  157.     /* the above are the logical dimensions of the subsampled image */
  158.   /* These values are computed before starting a scan of the component */
  159.     short MCU_width;    /* number of blocks per MCU, horizontally */
  160.     short MCU_height;    /* number of blocks per MCU, vertically */
  161.     short MCU_blocks;    /* MCU_width * MCU_height */
  162.     long subsampled_width;    /* image width in samples, after expansion */
  163.     long subsampled_height;    /* image height in samples, after expansion */
  164.     /* the above are the true_comp_xxx values rounded up to multiples of */
  165.     /* the MCU dimensions; these are the working dimensions of the array */
  166.     /* as it is passed through the DCT or IDCT step.  NOTE: these values */
  167.     /* differ depending on whether the component is interleaved or not!! */
  168. } jpeg_component_info;
  169.  
  170.  
  171. /* DCT coefficient quantization tables.
  172.  * For 8-bit precision, 'INT16' should be good enough for quantization values;
  173.  * for more precision, we go for the full 16 bits.  'INT16' provides a useful
  174.  * speedup on many machines (multiplication & division of JCOEFs by
  175.  * quantization values is a significant chunk of the runtime).
  176.  * Note: the values in a QUANT_TBL are always given in zigzag order.
  177.  */
  178. #ifdef EIGHT_BIT_SAMPLES
  179. typedef INT16 QUANT_VAL;    /* element of a quantization table */
  180. #else
  181. typedef UINT16 QUANT_VAL;    /* element of a quantization table */
  182. #endif
  183. typedef QUANT_VAL QUANT_TBL[DCTSIZE2];    /* A quantization table */
  184. typedef QUANT_VAL * QUANT_TBL_PTR;    /* pointer to same */
  185.  
  186.  
  187. typedef struct {        /* A Huffman coding table */
  188.   /* These two fields directly represent the contents of a JPEG DHT marker */
  189.     UINT8 bits[17];        /* bits[k] = # of symbols with codes of */
  190.                 /* length k bits; bits[0] is unused */
  191.     UINT8 huffval[256];    /* The symbols, in order of incr code length */
  192.   /* This field is used only during compression.  It's initialized FALSE when
  193.    * the table is created, and set TRUE when it's been output to the file.
  194.    */
  195.     boolean sent_table;    /* TRUE when table has been output */
  196.   /* The remaining fields are computed from the above to allow more efficient
  197.    * coding and decoding.  These fields should be considered private to the
  198.    * Huffman compression & decompression modules.
  199.    */
  200.     UINT16 ehufco[256];    /* code for each symbol */
  201.     char ehufsi[256];    /* length of code for each symbol */
  202.     UINT16 mincode[17];    /* smallest code of length k */
  203.     INT32 maxcode[18];    /* largest code of length k (-1 if none) */
  204.     /* maxcode[17] is a sentinel to ensure huff_DECODE terminates */
  205.     short valptr[17];    /* huffval[] index of 1st symbol of length k */
  206. } HUFF_TBL;
  207.  
  208.  
  209. #define NUM_QUANT_TBLS      4    /* quantization tables are numbered 0..3 */
  210. #define NUM_HUFF_TBLS       4    /* Huffman tables are numbered 0..3 */
  211. #define NUM_ARITH_TBLS      16    /* arith-coding tables are numbered 0..15 */
  212. #define MAX_COMPS_IN_SCAN   4    /* JPEG limit on # of components in one scan */
  213. #define MAX_SAMP_FACTOR     4    /* JPEG limit on sampling factors */
  214. #define MAX_BLOCKS_IN_MCU   10    /* JPEG limit on # of blocks in an MCU */
  215.  
  216.  
  217. /* Working data for compression */
  218.  
  219. struct compress_info_struct {
  220. /*
  221.  * All of these fields shall be established by the user interface before
  222.  * calling jpeg_compress, or by the input_init or c_ui_method_selection
  223.  * methods.
  224.  * Most parameters can be set to reasonable defaults by j_c_defaults.
  225.  * Note that the UI must supply the storage for the main methods struct,
  226.  * though it sets only a few of the methods there.
  227.  */
  228.     compress_methods_ptr methods; /* Points to list of methods to use */
  229.  
  230.     external_methods_ptr emethods; /* Points to list of methods to use */
  231.  
  232.     IFILEREF input_file;    /* tells input routines where to read image */
  233.     JFILEREF output_file;    /* tells output routines where to write JPEG */
  234.  
  235.     long image_width;    /* input image width */
  236.     long image_height;    /* input image height */
  237.     short input_components;    /* # of color components in input image */
  238.  
  239.     short data_precision;    /* bits of precision in image data */
  240.  
  241.     COLOR_SPACE in_color_space; /* colorspace of input file */
  242.     COLOR_SPACE jpeg_color_space; /* colorspace of JPEG file */
  243.  
  244.     double input_gamma;    /* image gamma of input file */
  245.  
  246.     boolean write_JFIF_header; /* should a JFIF marker be written? */
  247.     /* These three values are not used by the JPEG code, only copied */
  248.     /* into the JFIF APP0 marker.  density_unit can be 0 for unknown, */
  249.     /* 1 for dots/inch, or 2 for dots/cm.  Note that the pixel aspect */
  250.     /* ratio is defined by X_density/Y_density even when density_unit=0. */
  251.     UINT8 density_unit;    /* JFIF code for pixel size units */
  252.     UINT16 X_density;    /* Horizontal pixel density */
  253.     UINT16 Y_density;    /* Vertical pixel density */
  254.  
  255.     short num_components;    /* # of color components in JPEG image */
  256.     jpeg_component_info * comp_info;
  257.     /* comp_info[i] describes component that appears i'th in SOF */
  258.  
  259.     QUANT_TBL_PTR quant_tbl_ptrs[NUM_QUANT_TBLS];
  260.     /* ptrs to coefficient quantization tables, or NULL if not defined */
  261.  
  262.     HUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  263.     HUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  264.     /* ptrs to Huffman coding tables, or NULL if not defined */
  265.  
  266.     UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arithmetic-coding tables */
  267.     UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arithmetic-coding tables */
  268.     UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arithmetic-coding tables */
  269.  
  270.     boolean arith_code;    /* TRUE=arithmetic coding, FALSE=Huffman */
  271.     boolean interleave;    /* TRUE=interleaved output, FALSE=not */
  272.     boolean optimize_coding; /* TRUE=optimize entropy encoding parms */
  273.     boolean CCIR601_sampling; /* TRUE=first samples are cosited */
  274.  
  275.     UINT16 restart_interval;/* MDUs per restart interval, or 0 for no restart */
  276.  
  277. /*
  278.  * These fields are computed during jpeg_compress startup
  279.  */
  280.     short max_h_samp_factor; /* largest h_samp_factor */
  281.     short max_v_samp_factor; /* largest v_samp_factor */
  282.  
  283. /*
  284.  * These fields are valid during any one scan
  285.  */
  286.     short comps_in_scan;    /* # of JPEG components output this time */
  287.     jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  288.     /* *cur_comp_info[i] describes component that appears i'th in SOS */
  289.  
  290.     long MCUs_per_row;    /* # of MCUs across the image */
  291.     long MCU_rows_in_scan;    /* # of MCU rows in the image */
  292.  
  293.     short blocks_in_MCU;    /* # of DCT blocks per MCU */
  294.     short MCU_membership[MAX_BLOCKS_IN_MCU];
  295.     /* MCU_membership[i] is index in cur_comp_info of component owning */
  296.     /* i'th block in an MCU */
  297.  
  298.     /* these fields are private data for the entropy encoder */
  299.     JCOEF last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each comp */
  300.     JCOEF last_dc_diff[MAX_COMPS_IN_SCAN]; /* last DC diff for each comp */
  301.     UINT16 restarts_to_go;    /* MDUs left in this restart interval */
  302.     short next_restart_num;    /* # of next RSTn marker (0..7) */
  303. };
  304.  
  305. typedef struct compress_info_struct * compress_info_ptr;
  306.  
  307.  
  308. /* Working data for decompression */
  309.  
  310. struct decompress_info_struct {
  311. /*
  312.  * These fields shall be established by the user interface before
  313.  * calling jpeg_decompress.
  314.  * Most parameters can be set to reasonable defaults by j_d_defaults.
  315.  * Note that the UI must supply the storage for the main methods struct,
  316.  * though it sets only a few of the methods there.
  317.  */
  318.     decompress_methods_ptr methods; /* Points to list of methods to use */
  319.  
  320.     external_methods_ptr emethods; /* Points to list of methods to use */
  321.  
  322.     JFILEREF input_file;    /* tells input routines where to read JPEG */
  323.     IFILEREF output_file;    /* tells output routines where to write image */
  324.  
  325.     /* these can be set at d_ui_method_selection time: */
  326.  
  327.     COLOR_SPACE out_color_space; /* colorspace of output */
  328.  
  329.     double output_gamma;    /* image gamma wanted in output */
  330.  
  331.     boolean quantize_colors; /* T if output is a colormapped format */
  332.     /* the following are ignored if not quantize_colors: */
  333.     boolean two_pass_quantize;    /* use two-pass color quantization? */
  334.     boolean use_dithering;        /* want color dithering? */
  335.     int desired_number_of_colors;    /* number of colors to use */
  336.  
  337.     boolean do_block_smoothing; /* T = apply cross-block smoothing */
  338.     boolean do_pixel_smoothing; /* T = apply post-subsampling smoothing */
  339.  
  340. /*
  341.  * These fields are used for efficient buffering of data between read_jpeg_data
  342.  * and the entropy decoding object.  By using a shared buffer, we avoid copying
  343.  * data and eliminate the need for an "unget" operation at the end of a scan.
  344.  * The actual source of the data is known only to read_jpeg_data; see the
  345.  * JGETC macro, below.
  346.  * Note: the user interface is expected to allocate the input_buffer and
  347.  * initialize bytes_in_buffer to 0.  Also, for JFIF/raw-JPEG input, the UI
  348.  * actually supplies the read_jpeg_data method.
  349.  */
  350.     char * input_buffer;    /* start of buffer (private to input code) */
  351.     char * next_input_byte;    /* => next byte to read from buffer */
  352.     int bytes_in_buffer;    /* # of bytes remaining in buffer */
  353.  
  354. /*
  355.  * These fields are set by read_file_header or read_scan_header
  356.  */
  357.     long image_width;    /* overall image width */
  358.     long image_height;    /* overall image height */
  359.  
  360.     short data_precision;    /* bits of precision in image data */
  361.  
  362.     COLOR_SPACE jpeg_color_space; /* colorspace of JPEG file */
  363.  
  364.         /* These three values are not used by the JPEG code, merely copied */
  365.     /* from the JFIF APP0 marker (if any). */
  366.     UINT8 density_unit;    /* JFIF code for pixel size units */
  367.     UINT16 X_density;    /* Horizontal pixel density */
  368.     UINT16 Y_density;    /* Vertical pixel density */
  369.  
  370.     short num_components;    /* # of color components in JPEG image */
  371.     jpeg_component_info * comp_info;
  372.     /* comp_info[i] describes component that appears i'th in SOF */
  373.  
  374.     QUANT_TBL_PTR quant_tbl_ptrs[NUM_QUANT_TBLS];
  375.     /* ptrs to coefficient quantization tables, or NULL if not defined */
  376.  
  377.     HUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  378.     HUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  379.     /* ptrs to Huffman coding tables, or NULL if not defined */
  380.  
  381.     UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
  382.     UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
  383.     UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
  384.  
  385.     boolean arith_code;    /* TRUE=arithmetic coding, FALSE=Huffman */
  386.     boolean CCIR601_sampling; /* TRUE=first samples are cosited */
  387.  
  388.     UINT16 restart_interval;/* MDUs per restart interval, or 0 for no restart */
  389.  
  390. /*
  391.  * These fields are computed during jpeg_decompress startup
  392.  */
  393.     short max_h_samp_factor; /* largest h_samp_factor */
  394.     short max_v_samp_factor; /* largest v_samp_factor */
  395.  
  396.     short color_out_comps;    /* # of color components output by color_convert */
  397.                 /* (need not match num_components) */
  398.     short final_out_comps;    /* # of color components in output image */
  399.     /* (1 when quantizing colors, else same as color_out_comps) */
  400.  
  401. /*
  402.  * These fields are valid during any one scan
  403.  */
  404.     short comps_in_scan;    /* # of JPEG components input this time */
  405.     jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  406.     /* *cur_comp_info[i] describes component that appears i'th in SOS */
  407.  
  408.     long MCUs_per_row;    /* # of MCUs across the image */
  409.     long MCU_rows_in_scan;    /* # of MCU rows in the image */
  410.  
  411.     short blocks_in_MCU;    /* # of DCT blocks per MCU */
  412.     short MCU_membership[MAX_BLOCKS_IN_MCU];
  413.     /* MCU_membership[i] is index in cur_comp_info of component owning */
  414.     /* i'th block in an MCU */
  415.  
  416.     /* these fields are private data for the entropy encoder */
  417.     JCOEF last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each comp */
  418.     JCOEF last_dc_diff[MAX_COMPS_IN_SCAN]; /* last DC diff for each comp */
  419.     UINT16 restarts_to_go;    /* MDUs left in this restart interval */
  420.     short next_restart_num;    /* # of next RSTn marker (0..7) */
  421. };
  422.  
  423. typedef struct decompress_info_struct * decompress_info_ptr;
  424.  
  425.  
  426. /* Macros for reading data from the decompression input buffer */
  427.  
  428. #ifdef CHAR_IS_UNSIGNED
  429. #define JGETC(cinfo)    ( --(cinfo)->bytes_in_buffer < 0 ? \
  430.              (*(cinfo)->methods->read_jpeg_data) (cinfo) : \
  431.              (int) (*(cinfo)->next_input_byte++) )
  432. #else
  433. #define JGETC(cinfo)    ( --(cinfo)->bytes_in_buffer < 0 ? \
  434.              (*(cinfo)->methods->read_jpeg_data) (cinfo) : \
  435.              (int) (*(cinfo)->next_input_byte++) & 0xFF )
  436. #endif
  437.  
  438. #define JUNGETC(ch,cinfo)  ((cinfo)->bytes_in_buffer++, \
  439.                 *(--((cinfo)->next_input_byte)) = (ch))
  440.  
  441. #define MIN_UNGET    4    /* may always do at least 4 JUNGETCs */
  442.  
  443.  
  444. /* A virtual image has a control block whose contents are private to the
  445.  * memory manager module (and may differ between managers).  The rest of the
  446.  * code only refers to virtual images by these pointer types, and never
  447.  * dereferences the pointer.
  448.  */
  449.  
  450. typedef struct big_sarray_control * big_sarray_ptr;
  451. typedef struct big_barray_control * big_barray_ptr;
  452.  
  453. /* Although a real ANSI C compiler can deal perfectly well with pointers to
  454.  * unspecified structures (see "incomplete types" in the spec), a few pre-ANSI
  455.  * and pseudo-ANSI compilers get confused.  To keep one of these bozos happy,
  456.  * add -DINCOMPLETE_TYPES_BROKEN to CFLAGS in your Makefile.  Then we will
  457.  * pseudo-define the structs as containing a single "dummy" field.
  458.  * The memory manager(s) #define AM_MEMORY_MANAGER before including this file,
  459.  * so that they can make their own definitions of the structs.
  460.  */
  461.  
  462. #ifdef INCOMPLETE_TYPES_BROKEN
  463. #ifndef AM_MEMORY_MANAGER
  464. struct big_sarray_control { long dummy; };
  465. struct big_barray_control { long dummy; };
  466. #endif
  467. #endif
  468.  
  469.  
  470. /* Method types that need typedefs */
  471.  
  472. typedef METHOD(void, MCU_output_method_ptr, (compress_info_ptr cinfo,
  473.                          JBLOCK *MCU_data));
  474. typedef METHOD(void, MCU_output_caller_ptr, (compress_info_ptr cinfo,
  475.                          MCU_output_method_ptr output_method));
  476. typedef METHOD(void, subsample_ptr, (compress_info_ptr cinfo,
  477.                      int which_component,
  478.                      long input_cols, int input_rows,
  479.                      long output_cols, int output_rows,
  480.                      JSAMPARRAY above,
  481.                      JSAMPARRAY input_data,
  482.                      JSAMPARRAY below,
  483.                      JSAMPARRAY output_data));
  484. typedef METHOD(void, unsubsample_ptr, (decompress_info_ptr cinfo,
  485.                        int which_component,
  486.                        long input_cols, int input_rows,
  487.                        long output_cols, int output_rows,
  488.                        JSAMPARRAY above,
  489.                        JSAMPARRAY input_data,
  490.                        JSAMPARRAY below,
  491.                        JSAMPARRAY output_data));
  492. typedef METHOD(void, quantize_method_ptr, (decompress_info_ptr cinfo,
  493.                        int num_rows,
  494.                        JSAMPIMAGE input_data,
  495.                        JSAMPARRAY output_workspace));
  496. typedef METHOD(void, quantize_caller_ptr, (decompress_info_ptr cinfo,
  497.                        quantize_method_ptr quantize_method));
  498.  
  499.  
  500. /* These structs contain function pointers for the various JPEG methods. */
  501.  
  502. /* Routines to be provided by the surrounding application, rather than the
  503.  * portable JPEG code proper.  These are the same for compression and
  504.  * decompression.
  505.  */
  506.  
  507. struct external_methods_struct {
  508.     /* User interface: error exit and trace message routines */
  509.     /* NOTE: the string msgtext parameters will eventually be replaced */
  510.     /* by an enumerated-type code so that non-English error messages */
  511.     /* can be substituted easily.  This will not be done until all the */
  512.     /* code is in place, so that we know what messages are needed. */
  513.     METHOD(void, error_exit, (const char *msgtext));
  514.     METHOD(void, trace_message, (const char *msgtext));
  515.  
  516.     /* Working data for error/trace facility */
  517.     /* See macros below for the usage of these variables */
  518.     int trace_level;    /* level of detail of tracing messages */
  519.     /* Use level 0 for unsuppressable messages (nonfatal errors) */
  520.     /* Use levels 1, 2, 3 for successively more detailed trace options */
  521.  
  522.     int message_parm[8];    /* store numeric parms for messages here */
  523.  
  524.     /* Memory management */
  525.     /* NB: alloc routines never return NULL. They exit to */
  526.     /* error_exit if not successful. */
  527.     METHOD(void *, alloc_small, (size_t sizeofobject));
  528.     METHOD(void, free_small, (void *ptr));
  529. #ifdef NEED_FAR_POINTERS    /* routines for getting far-heap space */
  530.     METHOD(void FAR *, alloc_medium, (size_t sizeofobject));
  531.     METHOD(void, free_medium, (void FAR *ptr));
  532. #else
  533. #define alloc_medium alloc_small
  534. #define free_medium  free_small
  535. #endif
  536.     METHOD(JSAMPARRAY, alloc_small_sarray, (long samplesperrow,
  537.                         long numrows));
  538.     METHOD(void, free_small_sarray, (JSAMPARRAY ptr,
  539.                      long numrows));
  540.     METHOD(JBLOCKARRAY, alloc_small_barray, (long blocksperrow,
  541.                          long numrows));
  542.     METHOD(void, free_small_barray, (JBLOCKARRAY ptr,
  543.                      long numrows));
  544.     METHOD(big_sarray_ptr, request_big_sarray, (long samplesperrow,
  545.                             long numrows,
  546.                             long unitheight));
  547.     METHOD(big_barray_ptr, request_big_barray, (long blocksperrow,
  548.                             long numrows,
  549.                             long unitheight));
  550.     METHOD(void, alloc_big_arrays, (long extra_small_samples,
  551.                     long extra_small_blocks,
  552.                     long extra_medium_space));
  553.     METHOD(JSAMPARRAY, access_big_sarray, (big_sarray_ptr ptr,
  554.                            long start_row,
  555.                            boolean writable));
  556.     METHOD(JBLOCKARRAY, access_big_barray, (big_barray_ptr ptr,
  557.                         long start_row,
  558.                         boolean writable));
  559.     METHOD(void, free_big_sarray, (big_sarray_ptr ptr));
  560.     METHOD(void, free_big_barray, (big_barray_ptr ptr));
  561. };
  562.  
  563. /* Macros to simplify using the error and trace message stuff */
  564. /* The first parameter is generally cinfo->emethods */
  565.  
  566. #define ERREXIT(emeth,msg)        ((*(emeth)->error_exit) (msg))
  567. #define ERREXIT1(emeth,msg,p1)        ((emeth)->message_parm[0] = (p1), \
  568.                      (*(emeth)->error_exit) (msg))
  569. #define ERREXIT2(emeth,msg,p1,p2)    ((emeth)->message_parm[0] = (p1), \
  570.                      (emeth)->message_parm[1] = (p2), \
  571.                      (*(emeth)->error_exit) (msg))
  572. #define ERREXIT3(emeth,msg,p1,p2,p3)    ((emeth)->message_parm[0] = (p1), \
  573.                      (emeth)->message_parm[1] = (p2), \
  574.                      (emeth)->message_parm[2] = (p3), \
  575.                      (*(emeth)->error_exit) (msg))
  576. #define ERREXIT4(emeth,msg,p1,p2,p3,p4) ((emeth)->message_parm[0] = (p1), \
  577.                      (emeth)->message_parm[1] = (p2), \
  578.                      (emeth)->message_parm[2] = (p3), \
  579.                      (emeth)->message_parm[3] = (p4), \
  580.                      (*(emeth)->error_exit) (msg))
  581.  
  582. #define MAKESTMT(stuff)        do { stuff } while (0)
  583.  
  584. #define TRACEMS(emeth,lvl,msg)    \
  585.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  586.         (*(emeth)->trace_message) (msg); } )
  587. #define TRACEMS1(emeth,lvl,msg,p1)    \
  588.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  589.         (emeth)->message_parm[0] = (p1); \
  590.         (*(emeth)->trace_message) (msg); } )
  591. #define TRACEMS2(emeth,lvl,msg,p1,p2)    \
  592.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  593.         (emeth)->message_parm[0] = (p1); \
  594.         (emeth)->message_parm[1] = (p2); \
  595.         (*(emeth)->trace_message) (msg); } )
  596. #define TRACEMS3(emeth,lvl,msg,p1,p2,p3)    \
  597.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  598.         int * _mp = (emeth)->message_parm; \
  599.         *_mp++ = (p1); *_mp++ = (p2); *_mp = (p3); \
  600.         (*(emeth)->trace_message) (msg); } )
  601. #define TRACEMS4(emeth,lvl,msg,p1,p2,p3,p4)    \
  602.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  603.         int * _mp = (emeth)->message_parm; \
  604.         *_mp++ = (p1); *_mp++ = (p2); *_mp++ = (p3); *_mp = (p4); \
  605.         (*(emeth)->trace_message) (msg); } )
  606. #define TRACEMS8(emeth,lvl,msg,p1,p2,p3,p4,p5,p6,p7,p8)    \
  607.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  608.         int * _mp = (emeth)->message_parm; \
  609.         *_mp++ = (p1); *_mp++ = (p2); *_mp++ = (p3); *_mp++ = (p4); \
  610.         *_mp++ = (p5); *_mp++ = (p6); *_mp++ = (p7); *_mp = (p8); \
  611.         (*(emeth)->trace_message) (msg); } )
  612.  
  613.  
  614. /* Methods used during JPEG compression. */
  615.  
  616. struct compress_methods_struct {
  617.     /* Hook for user interface to get control after input_init */
  618.     METHOD(void, c_ui_method_selection, (compress_info_ptr cinfo));
  619.     /* Input image reading & conversion to standard form */
  620.     METHOD(void, input_init, (compress_info_ptr cinfo));
  621.     METHOD(void, get_input_row, (compress_info_ptr cinfo,
  622.                      JSAMPARRAY pixel_row));
  623.     METHOD(void, input_term, (compress_info_ptr cinfo));
  624.     /* Gamma and color space conversion */
  625.     METHOD(void, colorin_init, (compress_info_ptr cinfo));
  626.     METHOD(void, get_sample_rows, (compress_info_ptr cinfo,
  627.                        int rows_to_read,
  628.                        JSAMPIMAGE image_data));
  629.     METHOD(void, colorin_term, (compress_info_ptr cinfo));
  630.     /* Expand picture data at edges */
  631.     METHOD(void, edge_expand, (compress_info_ptr cinfo,
  632.                    long input_cols, int input_rows,
  633.                    long output_cols, int output_rows,
  634.                    JSAMPIMAGE image_data));
  635.     /* Subsample pixel values of a single component */
  636.     /* There can be a different subsample method for each component */
  637.     METHOD(void, subsample_init, (compress_info_ptr cinfo));
  638.     subsample_ptr subsample[MAX_COMPS_IN_SCAN];
  639.     METHOD(void, subsample_term, (compress_info_ptr cinfo));
  640.     /* Extract samples in MCU order, process & hand off to output_method */
  641.     /* The input is always exactly N MCU rows worth of data */
  642.     METHOD(void, extract_init, (compress_info_ptr cinfo));
  643.     METHOD(void, extract_MCUs, (compress_info_ptr cinfo,
  644.                     JSAMPIMAGE image_data,
  645.                     int num_mcu_rows,
  646.                     MCU_output_method_ptr output_method));
  647.     METHOD(void, extract_term, (compress_info_ptr cinfo));
  648.     /* Entropy encoding parameter optimization */
  649.     METHOD(void, entropy_optimize, (compress_info_ptr cinfo,
  650.                     MCU_output_caller_ptr source_method));
  651.     /* Entropy encoding */
  652.     METHOD(void, entropy_encoder_init, (compress_info_ptr cinfo));
  653.     METHOD(void, entropy_encode, (compress_info_ptr cinfo,
  654.                       JBLOCK *MCU_data));
  655.     METHOD(void, entropy_encoder_term, (compress_info_ptr cinfo));
  656.     /* JPEG file header construction */
  657.     METHOD(void, write_file_header, (compress_info_ptr cinfo));
  658.     METHOD(void, write_scan_header, (compress_info_ptr cinfo));
  659.     METHOD(void, write_jpeg_data, (compress_info_ptr cinfo,
  660.                        char *dataptr,
  661.                        int datacount));
  662.     METHOD(void, write_scan_trailer, (compress_info_ptr cinfo));
  663.     METHOD(void, write_file_trailer, (compress_info_ptr cinfo));
  664.     /* Pipeline control */
  665.     METHOD(void, c_pipeline_controller, (compress_info_ptr cinfo));
  666.     METHOD(void, entropy_output, (compress_info_ptr cinfo,
  667.                       char *dataptr,
  668.                       int datacount));
  669.     /* Overall control */
  670.     METHOD(void, c_per_scan_method_selection, (compress_info_ptr cinfo));
  671. };
  672.  
  673. /* Methods used during JPEG decompression. */
  674.  
  675. struct decompress_methods_struct {
  676.     /* Hook for user interface to get control after reading file header */
  677.     METHOD(void, d_ui_method_selection, (decompress_info_ptr cinfo));
  678.     /* JPEG file scanning */
  679.     /* Note: user interface supplies read_jpeg_data for JFIF/raw-JPEG
  680.      * reading.  For file formats that require random access (eg, TIFF)
  681.      * the JPEG file header module will override the UI read_jpeg_data.
  682.      */
  683.     METHOD(void, read_file_header, (decompress_info_ptr cinfo));
  684.     METHOD(boolean, read_scan_header, (decompress_info_ptr cinfo));
  685.     METHOD(int, read_jpeg_data, (decompress_info_ptr cinfo));
  686.     METHOD(void, read_scan_trailer, (decompress_info_ptr cinfo));
  687.     METHOD(void, read_file_trailer, (decompress_info_ptr cinfo));
  688.     /* Entropy decoding */
  689.     METHOD(void, entropy_decoder_init, (decompress_info_ptr cinfo));
  690.     METHOD(void, entropy_decode, (decompress_info_ptr cinfo,
  691.                       JBLOCK *MCU_data));
  692.     METHOD(void, entropy_decoder_term, (decompress_info_ptr cinfo));
  693.     /* MCU disassembly: fetch MCUs from entropy_decode, build coef array */
  694.     METHOD(void, disassemble_init, (decompress_info_ptr cinfo));
  695.     METHOD(void, disassemble_MCU, (decompress_info_ptr cinfo,
  696.                        JBLOCKIMAGE image_data));
  697.     METHOD(void, disassemble_term, (decompress_info_ptr cinfo));
  698.     /* Cross-block smoothing */
  699.     METHOD(void, smooth_coefficients, (decompress_info_ptr cinfo,
  700.                        jpeg_component_info *compptr,
  701.                        JBLOCKROW above,
  702.                        JBLOCKROW currow,
  703.                        JBLOCKROW below,
  704.                        JBLOCKROW output));
  705.     /* Un-subsample pixel values of a single component */
  706.     /* There can be a different unsubsample method for each component */
  707.     METHOD(void, unsubsample_init, (decompress_info_ptr cinfo));
  708.     unsubsample_ptr unsubsample[MAX_COMPS_IN_SCAN];
  709.     METHOD(void, unsubsample_term, (decompress_info_ptr cinfo));
  710.     /* Gamma and color space conversion */
  711.     METHOD(void, colorout_init, (decompress_info_ptr cinfo));
  712.     METHOD(void, color_convert, (decompress_info_ptr cinfo,
  713.                      int num_rows,
  714.                      JSAMPIMAGE input_data,
  715.                      JSAMPIMAGE output_data));
  716.     METHOD(void, colorout_term, (decompress_info_ptr cinfo));
  717.     /* Color quantization */
  718.     METHOD(void, color_quant_init, (decompress_info_ptr cinfo));
  719.     METHOD(void, color_quantize, (decompress_info_ptr cinfo,
  720.                       int num_rows,
  721.                       JSAMPIMAGE input_data,
  722.                       JSAMPARRAY output_data));
  723.     METHOD(void, color_quant_prescan, (decompress_info_ptr cinfo,
  724.                        int num_rows,
  725.                        JSAMPIMAGE image_data));
  726.     METHOD(void, color_quant_doit, (decompress_info_ptr cinfo,
  727.                     quantize_caller_ptr source_method));
  728.     METHOD(void, color_quant_term, (decompress_info_ptr cinfo));
  729.     /* Output image writing */
  730.     METHOD(void, output_init, (decompress_info_ptr cinfo));
  731.     METHOD(void, put_color_map, (decompress_info_ptr cinfo,
  732.                      int num_colors, JSAMPARRAY colormap));
  733.     METHOD(void, put_pixel_rows, (decompress_info_ptr cinfo,
  734.                       int num_rows,
  735.                       JSAMPIMAGE pixel_data));
  736.     METHOD(void, output_term, (decompress_info_ptr cinfo));
  737.     /* Pipeline control */
  738.     METHOD(void, d_pipeline_controller, (decompress_info_ptr cinfo));
  739.     /* Overall control */
  740.     METHOD(void, d_per_scan_method_selection, (decompress_info_ptr cinfo));
  741. };
  742.  
  743.  
  744. /* External declarations for routines that aren't called via method ptrs. */
  745. /* Note: use "j" as first char of names to minimize namespace pollution. */
  746. /* The PP macro hides prototype parameters from compilers that can't cope. */
  747.  
  748. #ifdef PROTO
  749. #define PP(arglist)    arglist
  750. #else
  751. #define PP(arglist)    ()
  752. #endif
  753.  
  754.  
  755. /* main entry for compression */
  756. EXTERN void jpeg_compress PP((compress_info_ptr cinfo));
  757.  
  758. /* default parameter setup for compression */
  759. EXTERN void j_c_defaults PP((compress_info_ptr cinfo, int quality,
  760.                  boolean force_baseline));
  761. EXTERN void j_monochrome_default PP((compress_info_ptr cinfo));
  762. EXTERN void j_set_quality PP((compress_info_ptr cinfo, int quality,
  763.                   boolean force_baseline));
  764. EXTERN void j_c_free_defaults PP((compress_info_ptr cinfo));
  765.  
  766. /* main entry for decompression */
  767. EXTERN void jpeg_decompress PP((decompress_info_ptr cinfo));
  768.  
  769. /* default parameter setup for decompression */
  770. EXTERN void j_d_defaults PP((decompress_info_ptr cinfo,
  771.                  boolean standard_buffering));
  772. EXTERN void j_d_free_defaults PP((decompress_info_ptr cinfo,
  773.                   boolean standard_buffering));
  774.  
  775. /* forward DCT */
  776. EXTERN void j_fwd_dct PP((DCTBLOCK data));
  777. /* inverse DCT */
  778. EXTERN void j_rev_dct PP((DCTBLOCK data));
  779.  
  780. /* utility routines in jutils.c */
  781. EXTERN long jround_up PP((long a, long b));
  782. EXTERN void jcopy_sample_rows PP((JSAMPARRAY input_array, int source_row,
  783.                   JSAMPARRAY output_array, int dest_row,
  784.                   int num_rows, long num_cols));
  785. EXTERN void jcopy_block_row PP((JBLOCKROW input_row, JBLOCKROW output_row,
  786.                 long num_blocks));
  787. EXTERN void jzero_far PP((void FAR * target, size_t bytestozero));
  788.  
  789. /* method selection routines for compression modules */
  790. EXTERN void jselcpipeline PP((compress_info_ptr cinfo)); /* jcpipe.c */
  791. EXTERN void jselchuffman PP((compress_info_ptr cinfo)); /* jchuff.c */
  792. EXTERN void jselcarithmetic PP((compress_info_ptr cinfo)); /* jcarith.c */
  793. EXTERN void jselexpand PP((compress_info_ptr cinfo)); /* jcexpand.c */
  794. EXTERN void jselsubsample PP((compress_info_ptr cinfo)); /* jcsample.c */
  795. EXTERN void jselcmcu PP((compress_info_ptr cinfo)); /* jcmcu.c */
  796. EXTERN void jselccolor PP((compress_info_ptr cinfo));    /* jccolor.c */
  797. /* The user interface should call one of these to select input format: */
  798. EXTERN void jselrgif PP((compress_info_ptr cinfo)); /* jrdgif.c */
  799. EXTERN void jselrppm PP((compress_info_ptr cinfo)); /* jrdppm.c */
  800. EXTERN void jselrrle PP((compress_info_ptr cinfo)); /* jrdrle.c */
  801. EXTERN void jselrtarga PP((compress_info_ptr cinfo)); /* jrdtarga.c */
  802. /* and one of these to select output header format: */
  803. EXTERN void jselwjfif PP((compress_info_ptr cinfo)); /* jwrjfif.c */
  804.  
  805. /* method selection routines for decompression modules */
  806. EXTERN void jseldpipeline PP((decompress_info_ptr cinfo)); /* jdpipe.c */
  807. EXTERN void jseldhuffman PP((decompress_info_ptr cinfo)); /* jdhuff.c */
  808. EXTERN void jseldarithmetic PP((decompress_info_ptr cinfo)); /* jdarith.c */
  809. EXTERN void jseldmcu PP((decompress_info_ptr cinfo)); /* jdmcu.c */
  810. EXTERN void jselbsmooth PP((decompress_info_ptr cinfo)); /* jbsmooth.c */
  811. EXTERN void jselunsubsample PP((decompress_info_ptr cinfo)); /* jdsample.c */
  812. EXTERN void jseldcolor PP((decompress_info_ptr cinfo));    /* jdcolor.c */
  813. EXTERN void jsel1quantize PP((decompress_info_ptr cinfo)); /* jquant1.c */
  814. EXTERN void jsel2quantize PP((decompress_info_ptr cinfo)); /* jquant2.c */
  815. /* The user interface should call one of these to select input format: */
  816. EXTERN void jselrjfif PP((decompress_info_ptr cinfo)); /* jrdjfif.c */
  817. /* and one of these to select output image format: */
  818. EXTERN void jselwgif PP((decompress_info_ptr cinfo)); /* jwrgif.c */
  819. EXTERN void jselwppm PP((decompress_info_ptr cinfo)); /* jwrppm.c */
  820. EXTERN void jselwrle PP((decompress_info_ptr cinfo)); /* jwrrle.c */
  821. EXTERN void jselwtarga PP((decompress_info_ptr cinfo)); /* jwrtarga.c */
  822.  
  823. /* method selection routines for system-dependent modules */
  824. EXTERN void jselerror PP((external_methods_ptr emethods)); /* jerror.c */
  825. EXTERN void jselvirtmem PP((external_methods_ptr emethods)); /* jvirtmem.c */
  826.  
  827. /* debugging hook in jvirtmem.c */
  828. #ifdef MEM_STATS
  829. EXTERN void j_mem_stats PP((void));
  830. #endif
  831.  
  832. /* Miscellaneous useful macros */
  833.  
  834. #undef MAX
  835. #define MAX(a,b)    ((a) > (b) ? (a) : (b))
  836. #undef MIN
  837. #define MIN(a,b)    ((a) < (b) ? (a) : (b))
  838.  
  839.  
  840. #define RST0    0xD0        /* RST0 marker code */
  841.